//
//  SAAddSpendingCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2021/1/11.
//

#import "SAAddSpendingCell.h"
#import "SAAddSpendingModel.h"

@interface SAAddSpendingCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *typeLab;
@property (nonatomic, strong) UIButton *typeBtn;
@property (nonatomic, strong) UITextField *priceTf;
@property (nonatomic, strong) UITextField *remarkTf;

@end

@implementation SAAddSpendingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)setModel:(SAAddSpendingModel *)model {
    _model = model;
    _typeLab.text = model.typeName;
    _priceTf.text = model.price;
    _remarkTf.text = model.remark;
}

-(void)clickedTypeBtn {
    !self.typeBlock ?: self.typeBlock();
}

- (void)textFieldEditChanged:(UITextField*)textField {
    if (self.priceTf == textField) {
        self.model.price = textField.text;
        !self.totalBlock ?: self.totalBlock();
    }else if (self.remarkTf == textField) {
        self.model.remark = textField.text;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (self.priceTf == textField && textField.text.length > 8) {
        return NO;
    }
    // 如果第一位是.则前面加上0.
    if((textField.text.length == 0) && [string isEqualToString:@"."]) {
        textField.text = @"0";
    }
    if([textField.text containsString:@"."]) {
        if ([string isEqualToString:@"."]) {
            return NO;
        }
        NSRange ran = [textField.text rangeOfString:@"."];
        if(range.location > ran.location) {
            if([textField.text pathExtension].length > 1) {
                return NO;
            }
        }
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}


#pragma mark - initUI
-(void)initUI {
    self.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:self.typeLab];
    [self.contentView addSubview:self.typeBtn];
    [self.contentView addSubview:self.priceTf];
    [self.contentView addSubview:self.remarkTf];
}

#pragma mark - lazy
- (UILabel *)typeLab {
    if (!_typeLab) {
        _typeLab = [[UILabel alloc] initWithFrame:SARectMake(30, 0, 55, 35)];
        _typeLab.textColor = kBlackColor;
        _typeLab.font = kFont(14);
        _typeLab.textAlignment = NSTextAlignmentLeft;
    }
    return _typeLab;
}

-(UIButton *)typeBtn {
    if (!_typeBtn) {
        _typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _typeBtn.frame = SARectMake(30, 0, 60, 35);
        [_typeBtn setImage:[UIImage imageNamed:@"down_arrow"] forState:UIControlStateNormal];
        _typeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_typeBtn addTarget:self action:@selector(clickedTypeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _typeBtn;
}

-(UITextField *)priceTf {
    if (!_priceTf) {
        _priceTf = [[UITextField alloc] initWithFrame:SARectMake(110, 0, 120, 35)];
        _priceTf.textColor = kBlackColor;
        _priceTf.font = kFont(14);
        _priceTf.placeholder = @"请输入金额";
        _priceTf.delegate = self;
        _priceTf.textAlignment = NSTextAlignmentCenter;
        _priceTf.keyboardType = UIKeyboardTypeDecimalPad;
        [_priceTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _priceTf;
}

-(UITextField *)remarkTf {
    if (!_remarkTf) {
        _remarkTf = [[UITextField alloc] initWithFrame:SARectMake(235, 0, 140, 35)];
        _remarkTf.textColor = kBlackColor;
        _remarkTf.font = kFont(14);
        _remarkTf.placeholder = @"请输入备注";
        _remarkTf.textAlignment = NSTextAlignmentCenter;
        [_remarkTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _remarkTf;
}

@end
